/**
 * post 1.0
 *
 * Copyright (c) 2009 www.01mvc.com
 *
 * http://www.01cms.com
 *
 * Depends:
 *	ui.core.js
 *	ui.draggable.js
 *	ui.resizable.js
 *  ui.dialog.js
 */
$.fn.post = function(options){

    var defaults = {
        defaultMsg: '<span class="ui-dialog-loading">&nbsp;</span>正在处理,请稍候...',
        sendUrl: "",
		goUrl : "",
		trueJs : "",
		falseJs : "",
		falseCheck : true,
		style : "padding:15px; line-height:20px; font-size:13px; text-align:center;",
        dataClass : "posted",
		dataType : "json",
        stayTime : 2,
		buttons : '',
		height : 'auto',
		width : 300,
		maxHeight : '',
		maxWidth : '',
		minHeight : '',
		minWidth : '',
		title : "提示信息",
		beforeclose : '',
		resizable : false,
		open : '',
		focus : '',
		position : 'center',
		dragStart : '',
		drag : '',
		dragStop : '',
		resizeStart : '',
		resize : '',
		resizeStop : '',
		close : function() {$(':submit').attr('disabled',false);},
		msgClass : 'ui-dialog-message'+$(".ui-dialog").length
	};
  
    var options = $.extend(defaults, options);
	var msgClass = options.msgClass;
	
	$(this).submit(function(){				

		$(':submit').attr('disabled',true);
		
		$('<div class="'+msgClass+'" style="'+options.style+'">'+options.defaultMsg+'</div>').dialog({
			buttons : options.buttons,
			height : options.height,
			width : options.width,
			maxHeight : options.maxHeight,
			maxWidth : options.maxWidth,
			minHeight : options.minHeight,
			minWidth : options.minWidth,
			title : options.title,
			beforeclose : options.beforeclose,
			resizable : options.resizable,
			open : options.open,
			focus : options.focus,
			position : options.position,
			dragStart : options.dragStart,
			drag : options.drag,
			dragStop : options.dragStop,
			resizeStart : options.resizeStart,
			buttons: options.buttons,
			close : options.close
		});

		$("."+options.dataClass).each(function(){
							   
			if(options.data == '')
			{
				and = '';
			}
			else
			{
				and = '&';
			}
			
			var value = '';
			
			if(this.type == 'checkbox' || this.type == 'radio')
			{
				if(this.checked)
				{
					options.data += and + this.name + '=' + this.value;
				}
			}
			else
			{
				options.data += and + this.name + '=' + this.value;
			}
		});		
		
		$.ajax( {
			type : 'post',
			dataType : options.dataType,
			data : options.data,
			url : options.sendUrl,
			success : function(response) 
			{
				$("."+msgClass).html(response.message);
			  
				if (response.success && options.goUrl != '')
				{
					window.setTimeout(function(){ window.location.href = options.goUrl;} ,options.stayTime * 1000);
				}
				else
				{
					window.setTimeout(function(){$(".ui-dialog:has(."+msgClass+")").hide('slow',function(){$(".ui-dialog:has(."+msgClass+")").remove();$("."+msgClass).remove();});$(':submit').attr('disabled',false);},options.stayTime*1000);
				}
				
				if (response.js != '')
				{
					eval(response.js);
				}
				
				if (options.falseCheck && !response.success)
				{
					$(".check").each(function(){
						if($(this).val() == '')
						{
							$(this).addClass("yellowBg");
							$(this).change(function(){
								if($(this).val() != '')
								{
									$(this).removeClass("yellowBg");
								}
								else
								{
									$(this).addClass("yellowBg");
								}
							});
						}
						else
						{
							$(this).removeClass("yellowBg");
						}
					});
				}
				
				if (response.success && options.trueJs != '')
				{
					eval(options.trueJs);
				}
				else if (options.falseJs != '')
				{
					eval(options.falseJs);
				}
			}
		});
		
		return false;
	});
};